from django.db import models
from django.contrib.auth.models import User

class DatabaseHost(models.Model):
    name = models.CharField(max_length=100)
    host = models.CharField(max_length=100)
    port = models.IntegerField(default=0)
    comment = models.CharField(max_length=255, null=True, blank=True)

    def __unicode__(self):
        if self.port == 0:
            return "%s (%s)" % (self.name, self.host)
        else:
            return "%s (%s:%s)" % (self.name, self.host, self.port)

class Database(models.Model):
    user = models.ForeignKey(User)
    host = models.ForeignKey(DatabaseHost)
    name = models.CharField(max_length=50)
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ["name"]
        unique_together = (("host", "name"),("host","username"))
